package game;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Hashtable;

/**
 *@author Robert Devereux
 *
 *Class to represent a single iteration of a round from when the tiles are dealt out to the reveal of the winner.
 *
 */	

public class Round
{
	ComputerAgent ca, ca1, ca2;
	PlayerAgent pa;
	DictionaryTable dt;
	Bag scrabbleBag;
	ArrayList<Object> playersArray; 
	int [] playersBet = new int[4];
	Boolean[] playerHasFolded = new Boolean[4];
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int index, pScore, caScore, caScore1, caScore2, minBet = 2, roundNo = 0; 
	public static int pot;
	Hashtable <String, String> ht = new Hashtable <String, String> ();

	public Round(ArrayList<Object> players, int indexLittleBlind) throws IOException 
	{
        players.add(pa);
        players.add(ca);
        players.add(ca1);
        players.add(ca2);
		playersArray = players;
		index = indexLittleBlind;

		System.out.println("*****Welcome to TEXAS SCRAMBLE!!!*****");

		ca.difficultyInt = ca1.difficultyInt = ca2.difficultyInt = ca.getDifficulty();
	}

	public static int getPot()
	{
		return pot;
	}

	public int resetPot()
	{
		return pot = 0;	
	}

	public void playerHasChecked()
	{
		
	}
	
	public void playerHasRaised()
	{
		
	}
	
	public void playerHasFold()
	{
		
	}
	
	public void firstBet()
	{

	}

	public void secondBet()

	{

	}
	
	public ArrayList<Object> Hand()
	{
		ArrayList<Object> players = null;
		
		dt = new DictionaryTable("dictionary.txt");
		scrabbleBag = new Bag();
		
		playerHasFolded[0] = false; 
		playerHasFolded[1] = false; 
		playerHasFolded[2] = false;
		playerHasFolded[3] = false;
		
		pa.rack.addLetter(scrabbleBag.getLetter(3));
		ca.rack.addLetter(scrabbleBag.getLetter(3));
		ca1.rack.addLetter(scrabbleBag.getLetter(3));
		ca2.rack.addLetter(scrabbleBag.getLetter(3));
		
		roundNo++;
		System.out.println("Welcome to round " + roundNo);

		char [] commonTiles = scrabbleBag.getLetter(3);

		pa.rack.addLetter(commonTiles);
		ca.rack.addLetter(commonTiles);
		ca1.rack.addLetter(commonTiles);
		ca2.rack.addLetter(commonTiles);
		
		System.out.println("Your rack of letters is: " + pa.rack.returnString());
		
		firstBet();
		
		pot = pot + pa.pBet + playersBet[0] + playersBet[1] + playersBet[2];
		System.out.println("The current pot is " + pot);

		pa.pBet = 0;
		playersBet[0] = 0;
		playersBet[1] = 0;
		playersBet[2] = 0;
		minBet = 0;
		
		char [] finalTiles = scrabbleBag.getLetter(2);

		pa.rack.addLetter(finalTiles);
		ca.rack.addLetter(finalTiles);
		ca1.rack.addLetter(finalTiles);
		ca2.rack.addLetter(finalTiles);
		
		System.out.println("Your rack of final letters is: " + pa.rack.returnString());
		
		secondBet();
		
		pot = pot + pa.pBet + playersBet[0] + playersBet[1] + playersBet[2];
		System.out.println("The final pot is " + pot);
		
		String pWord;
		pWord = pa.getWordInput();
		pa.validWords();

		while(!(pa.validWords().contains(pWord)))
		{
			System.out.println("Sorry, that wasn't a valid word. Please try again.");
			pWord = pa.getWordInput();
		}
		
		pScore = pa.getScore(pWord); 
		caScore = ca.getScore();
		caScore1 = ca1.getScore();
		caScore2 = ca2.getScore();

		System.out.println("User's score was "+ pScore);	
		System.out.println("Computer Player 1's word was " + ca.bestWord() + " with a score of " + caScore + "."); 
		System.out.println("Computer Player 2's word was " + ca1.bestWord() + " with a score of " + caScore1 + "."); 
		System.out.println("Computer Player 3's word was " + ca2.bestWord() + " with a score of " + caScore2 + "."); 
		
		checkWinner();

		System.out.println("Your current bankRoll is now " +pa.getCurrentBankRoll());
		System.out.println("");
		
		ca.rack.emptyRack();
		ca1.rack.emptyRack();
		ca2.rack.emptyRack();
		pa.rack.emptyRack();

		resetPot();
		pa.pBet = 0;
		minBet = 2;
		playersBet[0] = 0;
		playersBet[1] = 0;
		playersBet[2] = 0;
		
		return players;
	}	

	public void checkWinner()
	{
		if(playerHasFolded[1].equals("fold"))
		{
			caScore = 0;
		}

		if(playerHasFolded[2].equals("fold"))
		{
			caScore1 = 0;
		}

		if(playerHasFolded[3].equals("fold"))
		{
			caScore2 = 0;
		}

		if ((playerHasFolded[0].equals("fold")))
		{
			pScore = 0;
		}

		if((pScore > caScore) && (pScore > caScore1) && (pScore > caScore2))
		{
			System.out.println("Player wins this round!");
			pa.addToBankRoll(pot);
		}

		else if((caScore > caScore1) && (caScore > caScore2) && (caScore > pScore))
		{
			System.out.println("Computer Player 1 wins this round!");
			ca.addToBankRoll(pot);
		}

		else if((caScore1 > caScore) && (caScore1 > caScore2) && (caScore1 > pScore))
		{
			System.out.println("Computer Player 2 wins this round!");
			ca1.addToBankRoll(pot);
		}

		else if((caScore2 > caScore) && (caScore2 > caScore1) && (caScore2 > pScore))
		{
			System.out.println("Computer Player 3 wins this round!");
			ca2.addToBankRoll(pot);
		}

		else
		{
			System.out.println("The game was a tie! The pot will be split among winners!");

			if((pScore == caScore) && (pScore > caScore1) && (pScore > caScore2))
			{
				pa.addToBankRoll(pot/2);
				ca.addToBankRoll(pot/2);
				System.out.println("The pot will be split between you and Computer Player 1!");
			}

			if((pScore > caScore) && (pScore == caScore1) && (pScore > caScore2))
			{
				pa.addToBankRoll(pot/2);
				ca1.addToBankRoll(pot/2);
				System.out.println("The pot will be split between you and Computer Player 2!");
			}

			if((pScore > caScore) && (pScore > caScore1) && (pScore == caScore2))
			{
				pa.addToBankRoll(pot/2);
				ca2.addToBankRoll(pot/2);
				System.out.println("The pot will be split between you and Computer Player 3!");
			}

			if((caScore > pScore) && (caScore == caScore1) && (caScore > caScore2))
			{
				ca.addToBankRoll(pot/2);
				ca1.addToBankRoll(pot/2);
				System.out.println("The pot will be split between Computer Player 1 and Computer Player 2!");
			}

			if((caScore > pScore) && (caScore > caScore1) && (caScore == caScore2))
			{
				ca.addToBankRoll(pot/2);
				ca2.addToBankRoll(pot/2);
				System.out.println("The pot will be split between Computer Player 1 and Computer Player 3!");
			}

			if((caScore1 > pScore) && (caScore1 > caScore) && (caScore1 == caScore2))
			{
				ca1.addToBankRoll(pot/2);
				ca2.addToBankRoll(pot/2);
				System.out.println("The pot will be split between Computer Player 2 and Computer Player 3!");
			}

			if((caScore > pScore) && (caScore == caScore1) && (caScore == caScore2))
			{
				ca.addToBankRoll(pot/3);
				ca1.addToBankRoll(pot/3);
				ca2.addToBankRoll(pot/3);
				System.out.println("The pot will be split between Computer Player 1, Computer Player 2 and Computer Player 3!");
			}

			if((pScore == caScore) && (pScore == caScore1) && (pScore > caScore2))
			{
				pa.addToBankRoll(pot/3);
				ca.addToBankRoll(pot/3);
				ca1.addToBankRoll(pot/3);
				System.out.println("The pot will be split between you, Computer Player 1 and Computer Player 2!");
			}

			if((pScore == caScore) && (pScore > caScore1) && (pScore == caScore2))
			{
				pa.addToBankRoll(pot/3);
				ca.addToBankRoll(pot/3);
				ca2.addToBankRoll(pot/3);
				System.out.println("The pot will be split between you, Computer Player 1 and Computer Player 3!");
			}

			if((pScore > caScore) && (pScore == caScore1) && (pScore == caScore2))
			{
				pa.addToBankRoll(pot/3);
				ca1.addToBankRoll(pot/3);
				ca2.addToBankRoll(pot/3);
				System.out.println("The pot will be split between you, Computer Player 2 and Computer Player 3!");
			}

			if((pScore == caScore) && (pScore == caScore1) && (pScore == caScore2))
			{
				pa.addToBankRoll(pot/4);
				ca.addToBankRoll(pot/4);
				ca1.addToBankRoll(pot/4);
				ca2.addToBankRoll(pot/4);
				System.out.println("The pot will be split between all Players!");

			}
		}
	}
}
